package net.sureon.dao;

import java.util.List;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

public class GroupDAOImpl extends SqlMapClientDaoSupport implements GroupDAO {

	public Group getGroup(long companyID, long groupID) {
		Group group = new Group();
		group.setCompanyID(companyID);
		group.setGroupID(groupID);
		return (Group) getSqlMapClientTemplate().queryForObject("getGroupByID",
				group);
	}

	public List<Group> getGroupByCompanyID(long companyID) {
		return getSqlMapClientTemplate().queryForList("getGroupByCompanyID",
				companyID);
	}

	public long insertGroup(Group group) {
		Object o = getSqlMapClientTemplate().insert("insertGroup", group);
		if (o instanceof Long) {
			return ((Long) o);
		}
		return 0;
	}

	public int updateGroup(Group group) {
		return getSqlMapClientTemplate().update("updateGroup", group);
	}

    public List<Group> getChildGroups(long companyID, long parentGroupID) {
        Group group = new Group();
        group.setCompanyID(companyID);
        group.setParentGroup(parentGroupID);
        return getSqlMapClientTemplate().queryForList("getChildGroups",
            group);
    }

    public long getCompanyTopGroupID(long companyID) {
		return (Long) getSqlMapClientTemplate().queryForObject("getCompanyTopGroupID",
				companyID);
	}

	public int deleteNullGroup(long companyID, long groupID) {
		Group group = new Group();
		group.setGroupID(groupID);
		group.setCompanyID(companyID);
		return getSqlMapClientTemplate().delete("deleteNullGroup", group);
	}

	public List<Group> getGroupByName(long companyID, String groupName) {
		Group group = new Group();
		group.setCompanyID(companyID);
		group.setGroupName(groupName);
		return (List<Group>)getSqlMapClientTemplate().queryForList("getGroupByName", group);
	}

	public long getManagedGroup(long companyID, long userID) {
		Group vo = new Group();
		vo.setCompanyID(companyID);
		vo.setGroupManager(userID);
		Object o = getSqlMapClientTemplate().queryForObject("getManagedGroup", vo);
		if(o instanceof Long){
			return ((Long) o).longValue();
		}
		return 0;
	}

}
